using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using DG.Tweening;
public enum StateType {Patrol,Att, Track,Stand }

//人物巡逻的基本参数：这么写的啊
[Serializable]
public class LoopPath
{
    [Header("路径点")]
    public Transform Poitn;//路径点

    [Header("所需要的路径")]
    public Transform[] LoopPoitn;
    [Header("移动的时间")]
    public float MoveTime;//移动时间
    public float initMoveTime;
    [Header("在每个点等待的时间")]
    public float WaitTime;//等待时间
    public float initWaitTime;
    [Header("速度")]
    public Vector3 Speed;//移动速度



}



//敌人属性
[Serializable]
public class Attribute {
    //HP的UI显示
    public GameObject[] hpView;

    public Image hpImage;
    public Image huangchongHp;

    [Header("血量")]
    public int blood;
    [Header("攻击力")]
    public float aggress;
    [Header("行动点数")]
    public int point;
    //小状态图标/表情图标展示 //如果你需要放到任何一个地方，那就咋没做
  //  public SpriteRenderer sr;

    public Image stateIcon;
    public Sprite[] stateIconSprites;


}





public class FSM : MonoBehaviour
{


    [Header("当前状态")]
    private Istate currentState;

    [Header("当前状态-颜色")]
    private Color currentStateColor;

    //属性
    public Attribute attribute;
     
    public Material material;

    [Header("巡逻点")]
    public LoopPath[] loopPaths;
 
    //字典
    public Dictionary<StateType, Istate> states = new Dictionary<StateType, Istate>();

    public Dictionary<StateType,Color> stateColor=new Dictionary<StateType,Color>();


    //减血事件
    public void reduceHP(int i){
            
        //开启携程，白光效果
        StartCoroutine(Injurd());
        if(attribute.blood>0){
        
            while(i>0){
                //为啥要怎么写呢，方便更新血量的UI界面
                i--;
                attribute.blood--;
                 //更新血量UI；
                attribute.hpView[attribute.blood].SetActive(false);
                if (attribute.blood <= 0) {
                    Dead();
                }
     
                print("敌人减血");
             



            }

        }

    }

    public delegate int After();

    public int ReducePointAft(After aft ) {
        //回调函数

        return aft();
    }


    //减少点数
    public int ReducePoint(){
    
        if(attribute.point>0){
            attribute.point--;

            if (attribute.point == 0)
            {
                //该敌人的点数用完，行动结束
                GameManger.Instance.reducePoint();
            }

        }
        else if(attribute.point == 0)
        {
            //该敌人的点数用完，行动结束
            GameManger.Instance.reducePoint();
        }

        return attribute.point;
    }


    //TODO 死亡的方案Dead
    public void Dead(){

        GameManger.Instance.reduceLeaveEmenyNum(1);

        //进入自由探索模式，不再投色子，没有点数

        gameObject.SetActive(false);
    }

    Image hpImage;
    Image huangchongHp;

    private void Awake()
    {

         hpImage = attribute.hpImage;
         huangchongHp = attribute.huangchongHp;


        states.Add(StateType.Patrol, new Patrol(this));
        stateColor.Add(StateType.Patrol,new Color());

        states.Add(StateType.Track, new Track(this));
        stateColor.Add(StateType.Track,new Color());

        states.Add(StateType.Att, new Att(this));
        stateColor.Add(StateType.Att,new Color());

        states.Add(StateType.Stand,new Stand(this));
        stateColor.Add(StateType.Stand,new Color());

    }



    //检测范围 判断是要进入何种状态
    public void CheckRange()
    {

        float radius = 3;
        float dis = Vector3.Distance(Player.Instance.transform.position, gameObject.transform.position);
        if(dis <= radius){
            //从巡逻状态，切换为

           if(dis >= 1 && Player.Instance.LockedEmeny==null){
                //当切换到追踪状态的时候，要考虑到，该玩家是否已经被其他人追
                TransitionState(StateType.Track);
            }else{
                TransitionState(StateType.Att);
            }
        }
}


    // Start is called before the first frame update
    void Start()
    {
        material = gameObject.GetComponent<SpriteRenderer>().material;
    
        //先加一个出场的动画 动画器，登场，判断动画结束




       TransitionState(StateType.Stand);
    }


    //诞生光效结束之后，好像整个都不需要做哎，整个逻辑
    public void CreateAfter(){


    }


    // Update is called once per frame
    void Update()
    {

        transform.rotation = Camera.main.transform.rotation;

        if (currentState != null) {
            currentState.OnUpdate();
        }
        //持续执行，当前状态的update方法
      
    }


    public void TransitionState(StateType type)
    {
        if (currentState != null)
            currentState.OnExit();
        currentState = states[type];
        //当前状态所对应的颜色color
        currentStateColor=stateColor[type];
        //切换到，对应状态的颜色小提示 这个现在不一定要加
      //  attribute..DOColor(currentStateColor,0.2f);
        
        currentState.OnEnter();
    }

  //受伤，效果
    IEnumerator Injurd()
    {


        print("1111111123");
        yield return new WaitForSeconds(0.1f);
        material.SetFloat("_FlashAmount", 1f);
        yield return new WaitForSeconds(0.02f);
        material.SetFloat("_FlashAmount", 0f);
        print(hpImage.gameObject.GetComponent<RectTransform>().localScale.y - 0.3f);
        hpImage.gameObject.GetComponent<RectTransform>().DOScaleX(hpImage.gameObject.GetComponent<RectTransform>().localScale.x - 0.3f, 0.1f).SetEase(Ease.InExpo);
        huangchongHp.gameObject.GetComponent<RectTransform>().DOScaleX(huangchongHp.gameObject.GetComponent<RectTransform>().localScale.x - 0.3f, 0.2f).SetEase(Ease.InExpo);




    }








}

